<?php
/*
Plugin Name: Stumble Info Link
Version: 0.9
Plugin URI: http://leau.net/su/
Author: Edward de Leau
Author URI: http://edward.de.leau.net
Description: Places a grey or colored stumble-upon icon and link behind each url
License: Copyright 2008  edward de Leau  (email : deleau@gmail.com)

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
Installation:
	1. Upload the file in your plugins directory
	2. Activate the plugin from your WordPress admin 'Plugins' page.
Version History:
	v.0.1 primary version
	v.0.2 skipped .zip, .pdf
	v.0.3 added colors to the icon: grey if not in su, colored if it is
	v.0.4 changed to use local rss reader and cache instead of my server!
	v.0.5 bugfixed
	v.0.6 distinction between 0 and -1.
	v.0.7 several fixes
	v.0.8 added seperate function to use in your websites
	v.0.9 added text, icon, admin panel changes, new supericon
*/

include_once(ABSPATH . WPINC . '/rss.php');

$stumbleinfo_options = get_option('stumbleinfo');
add_action('admin_head', 'stumbleinfo_admin_head');

function stumbleinfo_admin_head()
{
    add_options_page('StumbleInfo', 'StumbleInfo', 'manage_options', 'stumbleinfo/options.php');
}

function checkSURating($feedUrl) {

	$shortHttpString = str_replace('http://', '', $feedUrl);
	$shortHttpString = str_replace('https://', '', $feedUrl);

	$feedUrl = 'http://www.stumbleupon.com/reviews.php?url=' . $feedUrl;
	$rss = fetch_rss("$feedUrl");
	if  ($rss->channel['title']) {
		return count($rss->items);
	} else {
		return -1;
	}
}

function strxchr($haystack, $needle, $l_inclusive = 0, $r_inclusive = 0){
   if (strrpos($haystack, $needle) !== false) {
       $left =  substr($haystack, 0, strrpos($haystack, $needle) + $l_inclusive);
       $r_inclusive = ($r_inclusive == 0) ? 1 : 0;
       $right =  substr(strrchr($haystack, $needle), $r_inclusive);
       $a = array($left, $right);
       return $a;
   } else{
       return false;
   }
}

function buildDisplayIcon($url, $title, $urlType) {
	 global $stumbleinfo_options;

	 $amountOfReviews = checkSURating($url);
	 if ($stumbleinfo_options['stumbleinfo_showicon']) {
		if ($amountOfReviews == -1) {
			$typeOfSuClass="wpsulink wpsulink_off";
			$ptext = "&nbsp;";
		} else if ($amountOfReviews > 9) {
			$typeOfSuClass="wpsulink wpsulink_super";
			$ptext = "&nbsp;";
		} else {
			$typeOfSuClass="wpsulink wpsulink_on";
			$ptext = $amountOfReviews;
		}
	 } else {
		$typeOfSuClass="wpsulink_none";
	 }

	 if ($urlType=='view') {
		$suburl = "url/";
	 } else if ($urlType=='submit') {
		$suburl = "submit?url=";
	 } else {
		$suburl = "submit?url=";
	 }

    $st_string  = ' <a class="' . $typeOfSuClass . '" href="http://www.stumbleupon.com/' . $suburl . $url . '" target="_blank"><span>';

	 // with icons do not show -1 or +10 texts
    if ($stumbleinfo_options['stumbleinfo_showicon']) {
		$st_string = $st_string . $ptext;
	 } else {
	 	if ($stumbleinfo_options['stumbleinfo_showtext'])  {
			if ($amountOfReviews == -1) {
				$descr = $stumbleinfo_options['stumbleinfo_loser'];
			} else if ($amountOfReviews > 9) {
				$descr = $stumbleinfo_options['stumbleinfo_winner'];
			} else {
				$descr = $stumbleinfo_options['stumbleinfo_beforesu'] . $amountOfReviews . $stumbleinfo_options['stumbleinfo_aftersu'];
			}

	 		$st_string = $st_string . $descr;
	 	}
	 }
    $st_string = $st_string . '</span></a>';
	 return $st_string;
}

function Replacer($matches) {
    $or_string  = '<a' . $matches[1] . 'href="' . $matches[2] . '"'.$matches[3].'>' . $matches[4] . '</a>';

	 $st_string = buildDisplayIcon($matches[2],'su','view');

    if (strstr ( $matches[4], '<' )) {
      return $or_string;
    } else {
      $foundstr = strxchr($matches[2], ".", 1, 1);
      if ($foundstr && (stristr('.zip .pdf .arj', $foundstr[1]) != FALSE)) {
            return $or_string;
      }
      return $or_string . $st_string;
    }
}

function add_stumble_icon ($text) {
    return preg_replace_callback('/<a(.*?)href="(.*?)"(.*?)>(.*?)<\\/a>/i', 'Replacer', $text);
}

function add_stylesheet() {
	$plugin_loc = '/wp-content/plugins/stumbleinfo/img/';
	$plugin_base = get_bloginfo('wpurl');
   $plugin_root = $plugin_base . $plugin_loc;

	echo "\n\t<style type=\"text/css\">\n";
	echo "a.wpsulink_off {text-decoration: none; font-size:10px; vertical-align: baseline; font-family: arial; font-weight: 900; color: #fff;}\n";
	echo "a.wpsulink_off span {background:#fff url(".$plugin_root."off.gif) no-repeat scroll bottom left; text-indent:17px; display:inline-block;width:35px;}\n";

	echo "a.wpsulink_on {text-decoration: none; font-size:10px; vertical-align: baseline; font-family: arial; font-weight: 900; color: #000;}\n";
	echo "a.wpsulink_on span {background:#fff url(".$plugin_root."on.gif) no-repeat scroll bottom left; text-indent:17px; display:inline-block;width:35px;}\n";

	echo "a.wpsulink_super {text-decoration: none; font-size:10px; vertical-align: baseline; font-family: arial; font-weight: 900; color: #000;}\n";
	echo "a.wpsulink_super span {background:#fff url(".$plugin_root."super.gif) no-repeat scroll bottom left; text-indent:17px; display:inline-block;width:35px;}\n";
	echo "</style>\n";
}

//
// Call this function in your templates!
//
function stumbleInfoButton($url,$title='su',$urlType='submit') {
	 echo buildDisplayIcon($url,$title,$urlType);
}

add_filter('the_content', 'add_stumble_icon', 9);
add_action('wp_head', 'add_stylesheet');
?>
